#
# Copyright 2008 Wepoco.  http://www.wepoco.org/
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from google.appengine.ext import db
from google.appengine.api import users, urlfetch
from google.appengine.ext import webapp

from GPXTimeline import GPXTimeline
from Picasa import Picasa

#
# A very simple wrapper that returns a Simile Timeline compatible
# XML data file.
#

class Data(webapp.RequestHandler):
  def get(self):

    self.response.headers['Content-Type'] = 'application/xml'
    self.response.headers['Cache-Control'] = 'no-cache'
    self.response.headers['Content-Disposition']='attachment;filename=timeline.xml' 

    track_key = self.request.get('key')
    owner = self.request.get('owner')
    album = self.request.get('album')
    slip = self.request.get('slip')
    track = db.get(db.Key(track_key))

    if album:
      album_uri = "http://picasaweb.google.com/data/feed/api/user/%s/album/%s?kind=photo" % (owner,album)
    
      urlres = urlfetch.fetch(album_uri)
      if urlres.status_code != 200:
        return
      photos=Picasa(urlres.content).photos()
      GPXTimeline(track.getContent()).getPhotoLocations(photos, track.timeSlip)
      Picasa().dataXML(self.response.out, track_key, slip=slip, pics=photos)
      pass
    else:
      GPXTimeline().dataXML(track.getContent(),self.response.out)
      pass
    return
